package com.yy.young.travel.service.impl;

import java.math.BigDecimal;
import java.util.*;

import javax.annotation.Resource;

import com.yy.young.travel.model.NatureReserveCheck;
import com.yy.young.travel.service.INatureReserveCheckService;
import com.yy.young.travel.service.IReserveCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yy.young.dal.service.IDataAccessService;
import com.yy.young.dal.util.Page;
import com.yy.young.travel.model.Reserve;
import com.yy.young.travel.model.ReserveCategory;
import com.yy.young.travel.service.IReserveService;
import com.yy.young.travel.statisticbean.NcStatisc;

import net.sf.json.JSONObject;

/**
* 自然保护地详情表实体类服务实现
* Created by rookie on 2018-04-03.
*/
@Service("reserveService")
public class ReserveServiceImpl implements IReserveService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务
    

    private static final Logger logger = LoggerFactory.getLogger(ReserveServiceImpl.class);

    private static final String MAPPER = "com.yy.young.travel.mapper.reserve";//mapper的namespace

    //获取数据列表
    @Override
    public List<Reserve> getList(Reserve obj) throws Exception {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<Reserve> getPage(Reserve obj, Page page) throws Exception {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public Reserve get(String id) throws Exception {
        return (Reserve)dataAccessService.getObject(MAPPER + ".get", id);
    }

    //修改
    @Override
    public int update(Reserve obj) throws Exception {
    	
    	 return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) throws Exception {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }
	@Autowired
	private INatureReserveCheckService iNatureReserveCheckService;
	@Autowired
	private IReserveCategoryService iReserveCategoryService;
    //插入
    @Override
    public int insert(Reserve obj) throws Exception {
    	//校验输入坐标是否合法
		if("".equals(obj.getId())){
            //新增生成uuid
			String uuid = UUID.randomUUID().toString().replace("-", "");
			obj.setId(uuid);
		}
		obj.setState("1");
		//如果没输入面积则设置面积为0 防止页面出现null值
		if(obj.getArea()==null){
			obj.setArea(0d);
		}
		obj.setCreateTime(new Date());
		//向自然保护地审批表中插入数据
		NatureReserveCheck natureReserveCheck = new NatureReserveCheck();
		natureReserveCheck.setId(obj.getId());
		natureReserveCheck.setCreateTime(obj.getCreateTime());
		natureReserveCheck.setName(obj.getName());
		//插入自然保护地名称
		ReserveCategory reserveCategory = iReserveCategoryService.get(obj.getCategoryId());
		natureReserveCheck.setCategoryName(reserveCategory.getName());
		natureReserveCheck.setState("1");
		iNatureReserveCheckService.insert(natureReserveCheck);
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<Reserve> list) throws Exception {
        int i = 0;
        for(Reserve item : list){
            i += this.insert(item);
        }
        return i;
    }
    //保护地类型统计分析
  	public List ncStatis() throws Exception {
  		//list 集合[{CATEGORY_ID=1, N=2}, {CATEGORY_ID=4, N=1}, {CATEGORY_ID=3, N=1}]
  		List<Map> list = dataAccessService.getList(MAPPER + ".ncStatis", null);
  		List<NcStatisc> list1 = new ArrayList<NcStatisc>();
  		//遍历list集合 获取每一个map {CATEGORY_ID=1, N=2} 
  		for (Map map1 : list) {
  			Object id = map1.get("CATEGORY_ID");
  			BigDecimal bignum = (BigDecimal) map1.get("N");
  			int num = bignum.intValue();
			ReserveCategory rc = (ReserveCategory) dataAccessService.getObject("com.yy.young.travel.mapper.reserveCategory"+".get", id);
			//把name和值装进对象中
			NcStatisc nc = new NcStatisc();
			nc.setName(rc.getName());
			nc.setNum(num);
			//把对象存入list集合中
			list1.add(nc);
		}
  		
  		return list1;
  	}

	//保护地面积统计分析
	public List naStatis() throws Exception {
		//list 集合[{CATEGORY_ID=1, N=200}, {CATEGORY_ID=4, N=100}, {CATEGORY_ID=3, N=1}]
  		List<Map> list = dataAccessService.getList(MAPPER + ".naStatis", null);
  		List<NcStatisc> list1 = new ArrayList<NcStatisc>();
  		//遍历list集合 获取每一个map {CATEGORY_ID=1, N=2} 
  		for (Map map1 : list) {
  			Object id = map1.get("CATEGORY_ID");
  			BigDecimal area = (BigDecimal) map1.get("N");
			ReserveCategory rc = (ReserveCategory) dataAccessService.getObject("com.yy.young.travel.mapper.reserveCategory"+".get", id);
			//把name和值装进对象中
			NcStatisc nc = new NcStatisc();
			nc.setName(rc.getName());
			nc.setArea(area);
			//把对象存入list集合中
			list1.add(nc);
		}
  		return list1;
	}

	//所属部门统计分析
	public List deptStatis() throws Exception {
		List<Map> list = dataAccessService.getList(MAPPER + ".deptStatis", null);
		List<NcStatisc> list1 = new ArrayList<NcStatisc>();
  		for (Map map1 : list) {
  			//获取部门名称
  			String parentDept = (String) map1.get("PAREANT_DEPT");
  			BigDecimal bignum = (BigDecimal) map1.get("N");
  			int num = bignum.intValue();
			//把parentDept和值装进对象中
			NcStatisc nc = new NcStatisc();
			nc.setParentDept(parentDept);
			nc.setNum(num);
			//把对象存入list集合中
			list1.add(nc);
		}
		return list1;
	}
	//同意新增保护地申请 获得保护地id  以及审核意见  修改保护地状态  修改保护地审核实体类状态  设置checkResult
	public void agreeApply(NatureReserveCheck obj)throws Exception{
		Reserve reserve = this.get(obj.getId());
		reserve.setState("2");
		this.update(reserve);
		NatureReserveCheck natureReserveCheck = iNatureReserveCheckService.get(obj.getId());
		natureReserveCheck.setCheckResult("同意");
		natureReserveCheck.setCheckTime(new Date());
		natureReserveCheck.setOpinion(obj.getOpinion());
		natureReserveCheck.setState("2");
		iNatureReserveCheckService.update(natureReserveCheck);
	}

	//拒绝新增保护地申请 获得保护地id  以及审核意见  修改保护地状态  修改保护地审核实体类状态  设置checkResult
	public void disagreeApply(NatureReserveCheck obj)throws Exception{
		Reserve reserve = this.get(obj.getId());//根据id得到保护地信息
		reserve.setState("3");//设置状态为审批不通过
		this.update(reserve);//更新保护地
		NatureReserveCheck natureReserveCheck = iNatureReserveCheckService.get(obj.getId());
		natureReserveCheck.setCheckResult("拒绝");
		natureReserveCheck.setCheckTime(new Date());
		natureReserveCheck.setOpinion(obj.getOpinion());
		natureReserveCheck.setState("3");
		iNatureReserveCheckService.update(natureReserveCheck);
	}
}